package com.hxl.tech.gateway.auth.util;


import cn.hutool.core.util.StrUtil;
import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Path;
import com.querydsl.core.types.dsl.EntityPathBase;

import java.util.List;

public class QueryDslOrderUtil {

    public static <T> OrderSpecifier<?>[] orderByFields(EntityPathBase<T> pathBase, String sortType, List<String> sortFields) {
        OrderSpecifier<?>[] result = new OrderSpecifier[sortFields.size()];
        for (int i = 0; i < sortFields.size(); i++) {
            result[i] = orderByField(pathBase, sortType, sortFields.get(i));
        }
        return result;
    }

    public static <T> OrderSpecifier<?> orderByField(EntityPathBase<T> pathBase, String sortType, String sortField) {
        sortField = StringUtil.underLineToCamel(sortField);
        Order order = "asc".equalsIgnoreCase(sortType) ? Order.ASC : Order.DESC;
        Path<Object> fieldPath = com.querydsl.core.types.dsl.Expressions.path(Object.class, pathBase, StrUtil.toCamelCase(sortField));
        return new OrderSpecifier(order, fieldPath);
    }

}
